<template>
  <a-dropdown v-if='currentUser && currentUser.name' placement='bottomRight'>
    <span class='ant-pro-account-avatar'>
      <a-avatar size='small' :src="baseUrl + downloadAvatar({fileName: currentAvatar}) || 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png'"
                class='antd-pro-global-header-index-avatar' />
      <ellipsis :length='5' tooltip style='color: #fff;'>{{ currentUser.nickName }}</ellipsis>
    </span>
    <template v-slot:overlay>
      <a-menu class='ant-pro-drop-down menu' :selected-keys='[]'>
        <a-menu-item v-if='menu' key='center' @click='handleToCenter'>
          <a-icon type='user' />
          个人中心
        </a-menu-item>

        <a-menu-divider v-if='menu' />
        <a-menu-item key='logout' @click='handleLogout'>
          <a-icon type='logout' />
          退出登录
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size='small' :style='{ marginLeft: 8, marginRight: 8 }' />
  </span>
</template>

<script>
import { mapState } from 'vuex'
import { Modal } from 'ant-design-vue'
import { downloadAvatar } from '@/api/user'
import Ellipsis from '@/components/Ellipsis/Ellipsis.vue'
import { CURRENT_PATH, CURRENT_SELECT_APP, OPEN_MENU_KEY, SELECT_MENU } from '@/store/mutation-types'
let baseUrl = window.PROJECT_CONFIG.VUE_APP_API_BASE_URL
export default {
  name: 'AvatarDropdown',
  components: { Ellipsis },
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  data(){
    return {
      baseUrl
    }
  },
  computed: {
    ...mapState({
      avatar: state => state.user.avatar
    }),
    currentAvatar() {
      return window.decodeURI(this.avatar)
    }
  },
  methods: {
    downloadAvatar,
    handleToCenter() {
      this.$emit('hidden-side-menu')
      this.$router.push({ path: '/sys/personal' })
    },
    handleToSettings() {
      this.$router.push({ path: '/development/account/settings' })
    },
    handleLogout(e) {
      Modal.confirm({
        title: '提示',
        content: '您确定要注销吗？',
        onOk: () => {
          this.$emit('remove-mark-timer')
          this.$store.commit(OPEN_MENU_KEY, null)
          this.$store.commit(CURRENT_SELECT_APP, null)
          this.$store.commit(SELECT_MENU, null)
          this.$store.commit(CURRENT_PATH, null)
          return this.$store.dispatch('Logout').then(() => {
            this.$router.push({ name: 'login' })
          })
        },
        onCancel() {
        }
      })
    }
  }
}
</script>

<style lang='less' scoped>
.ant-pro-drop-down {
  :deep(.action) {
    margin-right: 8px;
  }

  :deep(.ant-dropdown-menu-item) {
    min-width: 160px;
  }
}
</style>
